package ioc.bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 会返回 3 个构造器。最终执行的，还是参数最多的那个构造器。
 *
 * 默认加在 ioc 容器中的组件，容器启动会调用无参构造器创建对象，再进行初始化赋值等操作
 *
 * @see AbstractAutowireCapableBeanFactory#createBeanInstance(String, RootBeanDefinition, Object[])
 * return autowireConstructor(beanName, mbd, ctors, args);
 */
@Component
public class Constructor {
    People people;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;

    @Autowired(required = false)
    public Constructor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Autowired(required = false)
    public Constructor(ApplicationContext applicationContext, BeanFactory beanFactory) {
        this.applicationContext = applicationContext;
        this.beanFactory = beanFactory;
    }

    public Constructor() {
        System.out.println("car constructor...");
    }

    /**
     * 构造器要用的组件，默认都是从容器中获取
     */
    public Constructor(People people) {
        this.people = people;
        System.out.println("Constructor...有参构造器");
    }

    public People getPeople() {
        return people;
    }

    /**
     * 标注在方法上，Spring 容器创建当前对象，就会调用此方法，方法使用的参数，自定义类型的值会从 ioc 容器中获取，完成赋值；
     */
    @Autowired
    public void setPeople(People people) {
        this.people = people;
    }

    /**
     * 标注在类上，调用方法时，方法使用的参数值从 ioc 容器中获取；
     */
    public void setPeople2(@Autowired People people) {
        this.people = people;
    }

    @Override
    public String toString() {
        return "People [people=" + people + "]";
    }
}
